package org.zpmis.common.utils.json;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class AbstractJsonMapper implements JsonMapper {

    private static final Logger log = LoggerFactory.getLogger(AbstractJsonMapper.class);

    private static final String CLASS_TYPE_GSON = "com.google.gson.Gson";

    public static AbstractJsonMapper initJsonMapper(JsonEnum jsonEnum) {
        switch (jsonEnum) {
            case GSON:
                if (isPresent(CLASS_TYPE_GSON)) {
                    log.info("used gson");
                    return new GsonMapper();
                } else {
                    log.info("gson not found");
                }
            default:
                return new GsonMapper();
        }
    }

    private static boolean isPresent(String className) {
        try {
            Class.forName(className);
            return true;
        } catch (IllegalAccessError err) {
            throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" +
                    className + "]: " + err.getMessage(), err);
        } catch (Throwable ex) {
            // Typically ClassNotFoundException or NoClassDefFoundError...
            return false;
        }
    }
}
